package com.vchecker.iobd_checker.broadcast;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.vchecker.iobd_checker.MainActivity;
import com.vchecker.iobd_checker.model.CommDefine;
import com.vchecker.iobd_checker.model.CommEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Yipb on 16-3-8.
 */
public class BluetoothSearchReceiver extends BroadcastReceiver {
    public  static final String TAG = "BluetoothSearchReceiver";
    public void onReceive(Context context, Intent intent) {
        try {

            String action = intent.getAction();
            Bundle b = intent.getExtras();
            if(b != null) {
                Object[] lstName = b.keySet().toArray();
            }

//        // 显示所有收到的消息及其细节
//        for (int i = 0; i < lstName.length; i++) {
//            String keyName = lstName[i].toString();
//            Log.e(keyName, String.valueOf(b.get(keyName)));
//        }
            BluetoothDevice device = null;
            // 搜索设备时，取得设备的MAC地址
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                device = intent
                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (device==null || !device.getName().equals("V-checker")) {
                    return;
                }
                if (device.getBondState() == BluetoothDevice.BOND_NONE) {
                    Bundle data = new Bundle();
                    data.putParcelable(BluetoothDevice.EXTRA_DEVICE,device);
                    EventBus.getDefault().post(new CommEvent(CommDefine.MESSAGE_SEARCH,CommDefine.MESSAGE_SEARCH_FOUND,data));
                }
            } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
                device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (device==null || !device.getName().equals("V-checker")) {
                    return;
                }
                switch (device.getBondState()) {
                    case BluetoothDevice.BOND_BONDING:
                        //MainActivity.addLog(Log.INFO, "正在配对......");
                        break;
                    case BluetoothDevice.BOND_BONDED:
                        CommDefine.addLog(TAG, "完成配对:" + device.getAddress());
                        //connect(device);//连接设备
                        EventBus.getDefault().post(new CommEvent(CommDefine.MESSAGE_CLIENT,CommDefine.MESSAGE_CLIENT_PAIR_FINISH));
                        break;
                    case BluetoothDevice.BOND_NONE:
                        CommDefine.addLog(TAG, "取消配对:" + device.getAddress());
                        EventBus.getDefault().post(new CommEvent(CommDefine.MESSAGE_CLIENT,CommDefine.MESSAGE_CLIENT_PAIR_CANCEL));
                    default:
                        break;
                }
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                CommDefine.addLog(TAG, "搜索完成");
                EventBus.getDefault().post(new CommEvent(CommDefine.MESSAGE_CLIENT,CommDefine.MESSAGE_CLIENT_SEARCH_FINISH));
            } else  if (intent.getAction().equals(
                    "android.bluetooth.device.action.PAIRING_REQUEST")){

                CommDefine.addLog(TAG, "请求配对：" + device.getAddress());
            }

        } catch (Exception ex){
            Log.i(TAG,ex.toString());
            EventBus.getDefault().post(new CommEvent(CommDefine.MESSAGE_CLIENT,CommDefine.MESSAGE_CLIENT_SEARCH_FINISH));
        }
    }
}
